﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ex5.OrderByWithLINQ
{
    class OrderByWithLINQ
    {
        class Student
        {
            public string FirstName { get; set; }
            public string LastName { get; set; }
        }

        static IEnumerable<Student> SortStudentsLINQ(Student[] students)
        {
            var sortedStudents =
                from student in students
                orderby student.FirstName, student.LastName descending
                select student;
            return sortedStudents;
        }

        static IEnumerable<Student> SortStudents(Student[] students)
        {
            return students.OrderBy(st => st.FirstName).ThenBy(student => student.LastName);
        }

        static void Main(string[] args)
        {
            Student[] students = new Student[] 
                                        { 
                                            new Student { FirstName = "Alex", LastName = "Buchkov" } ,
                                            new Student { FirstName = "Lisa", LastName = "Kirova" } ,
                                            new Student { FirstName = "Bobi", LastName = "Kirov" } 
                                        };

            //--Sort With LINQ-----------------------------------------

            var sortedStudentsLINQ = SortStudentsLINQ(students);
            if (sortedStudentsLINQ.Count() > 0)
            {
                Console.WriteLine("Sorted students with LINQ:\n");
            }
            foreach (var student in sortedStudentsLINQ)
            {
                Console.WriteLine("{0,-10}{1,-10}", student.FirstName, student.LastName);
            }
            Console.WriteLine();

            //--Sort With Extension Methods------------------------------

            var sortedStudents = SortStudents(students);
            if (sortedStudents.Count() > 0)
            {
                Console.WriteLine("Sorted students:\n");
            }
            foreach (var student in sortedStudents)
            {
                Console.WriteLine("{0,-10}{1,-10}", student.FirstName, student.LastName);
            }
            Console.WriteLine();
        }
    }
}
